import Vue from "vue";
import VueRouter from "vue-router";
import HelloWorld from "@/components/index.vue";
import Left from "@/components/left/left.vue";
import index from "@/components/index/index.vue";
import merchantCenter from "@/components/merchantCenter/merchantCenter.vue";
import channelInformation from "@/components/channelInformation/channelInformation.vue";
import orderListIndex from "@/components/orderList/orderListIndex.vue";
import complaintIndex from "@/components/complaint/complaintIndex.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "HelloWorld",
    component: HelloWorld,
    meta: { requiresAuth: false },
  },
  {
    path: "/left",
    name: "Left",
    component: Left,
    meta: { requiresAuth: false },
  },
  {
    path: "/index",
    name: "index",
    component: index,
    meta: { requiresAuth: true, title: "首页", icon: "el-icon-user-solid" },
  },
  {
    path: "/merchantCenter",
    name: "merchantCenter",
    component: merchantCenter,
    meta: { requiresAuth: true, title: "商户中心", icon: "el-icon-star-on" },
  },
  {
    path: "/channelInformation",
    name: "channelInformation",
    component: channelInformation,
    meta: {
      requiresAuth: true,
      title: "通道信息",
      icon: "el-icon-bell",
    },
  },
  {
    path: "/orderListIndex",
    name: "orderListIndex",
    component: orderListIndex,
    meta: { requiresAuth: true, title: "订单列表", icon: "el-icon-document" },
  },
  // {
  //   path: "/complaintIndex",
  //   name: "complaintIndex",
  //   component: complaintIndex,
  //   meta: {
  //     requiresAuth: true,
  //     title: "投诉列表",
  //     icon: "el-icon-document-copy",
  //   },
  // },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.name === "HelloWorld") {
    sessionStorage.clear(); //清除本地缓存
  } else {
    sessionStorage.setItem("myKey", JSON.stringify(to));
  }
  // 如果前往私有路由，则需要判断用户是否登录
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    const token = sessionStorage.getItem("Token");
    if (token) {
      next();
    } else {
      next({ path: "/" });
    }
  } else {
    next();
  }
});
export default router;
